<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>函数与方法的区别</h1>

<script>

    //1.函数与方法的区别。
    //下面定义的是函数
    /*
    function sayHello(){
        console.log('hello,world!');
    }

    let person = {
        name: '张三',
        age :20,
        //下面定义的是方法
        sayHello:function(){
            console.log('hello,world!');
        }
    }

    sayHello(); //调用函数
    person.sayHello(); //调用方法*/

    //2.this指向问题

    /*
    function sayHello() {
        console.log('hello,world!,my name is:' + this.name);
    }

    let person = {
        name: '张三',
        age: 20,
        //hello方法指向sayHello
        hello: sayHello
    }
    person.hello();
    sayHello();*/

    //使用apply或者call改变this指向问题。
    /*
    function sayHello() {
        console.log('hello,world!,my name is:' + this.name);
    }

    let person = {
        name: '张三',
        age: 20,
        //hello方法指向sayHello
        hello: sayHello
    }
    person.hello.apply(person,[]);
    sayHello.apply(person,[]);*/


    //call()把参数按顺序传入。
    function sayHello() {
        console.log('hello,world!,my name is:' + this.name);
    }

    let person = {
        name: '张三',
        age: 20,
        //hello方法指向sayHello
        hello: sayHello
    }
    person.hello.call(person,null);
    sayHello.call(person,null);
</script>
</body>
</html>